<?php
namespace app\modules\v1\controllers;
use Yii;
use yii\rest\ActiveController;
use yii\web\Response;
use app\modules\v1\models\Merchantextrastuff;
use app\modules\v1\models\MerchantWeixin;
use Qiniu\Auth;

class GetpictureController extends ActiveController
{
	
    public $modelClass = 'app\modules\v1\models\getpicture';
    
    public function actions()
    {
        $actions = parent::actions();
        unset($actions['index'], $actions['update'], $actions['create'], $actions['delete'], $actions['view']);
        return $actions;
    }

    public function actionCreate()
    {
    	$res = array();
    	if(Yii::$app->request->isPost){
    		$openid  = isset(Yii::$app->request->post()['openid'])?Yii::$app->request->post()['openid']:'';
    		if(empty($openid)){
    			$res['code'] = '10402';
    			$res['msg']  = '请取消关注后重新关注公众号。';
    			$response=Yii::$app->response;
    			$response->format=Response::FORMAT_JSON;
    			$response->data=$res;
    			return $response;
    		}
    		$pictures = array();
    		$weixininfo = MerchantWeixin::findByOpenid($openid);
    		if(!empty($weixininfo)){
    			$merchant_id  = $weixininfo->merchant_id;
    			$result = Merchantextrastuff::find()->where(['merchant_id'=>$merchant_id,'state'=>0])->all();
    			foreach ($result as $k => $value){
    				$qiniukey = $value->extra_stuff;
    				//获取图片
    				$dirqiniu 		= Yii::$app->basePath.'/web/Qiniu/';
    				require_once $dirqiniu. 'autoload.php';
    				$accessKey 		= Yii::$app->params['accessKey'];
    				$secretKey 		= Yii::$app->params['secretKey'];
    				$auth 			= new Auth($accessKey, $secretKey);
    				$baseUrl 		= 'http://images.yidong88.com/'.$qiniukey;
    				$authUrl 		= $auth->privateDownloadUrl($baseUrl);
    				$pictures[$k]['url'] 		=  $authUrl;
    				$pictures[$k]['qiniukey'] 	=  $qiniukey;
    				$pictures[$k]['pid'] 		=  $value->id;;
    			}
    		}
    		$res['data'] = $pictures;
    		$res['code'] = '10000';
    		$res['msg']  = '成功';
    		$response=Yii::$app->response;
    		$response->format=Response::FORMAT_JSON;
    		$response->data=$res;
    	}else{
    		$res['code'] = '10402';
    		$res['msg']  = '请取消关注后重新关注公众号。';
    		$response=Yii::$app->response;
    		$response->format=Response::FORMAT_JSON;
    		$response->data=$res;
    	} 
    }
}